package com.ymatou.shoppingcart.api.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.shoppingcart.api.parameter.ChangeCartProductNumBean;
import com.ymatou.shoppingcart.api.service.ChangeCartProductNumCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.database.ActivityInfo;
import com.ymttest.business.database.ProductInfo;
import com.ymttest.business.service.ShoppingCartCallServices;
import com.ymttest.database.mongo.ShopCartMongoBean;
import com.ymttest.database.mongo.ShoppingCartMongoWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改购物车中商品数量 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeCartProductNum {
	private static ChangeCartProductNumBean changenumbean;
	private static ChangeCartProductNumCall changenumcall;
	ShoppingCartMongoWapper scw;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改购物车中商品数量");
	}

	@Before
	public void caseUp() {
		changenumbean = new ChangeCartProductNumBean();
		changenumcall = new ChangeCartProductNumCall();
		try {
			scw = new ShoppingCartMongoWapper();
		} catch (Exception e) {
			Logger.setup("MongoDB初始化失败：" + e.getMessage(), -1);
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changenumcall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeCartProductNum_001() {
		Logger.start(true,"修改购物车中商品数量成功");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, 100);
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("从mongodb中把刚刚加入到购物车中的数据找出");
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			int count = 2;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(count, scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0).getNum(),
					"验证修改后的商品件数");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_002() {
		Logger.start(true,"count小于0,修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			int count = -1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.ERROR400_MSG_4), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_4);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_003() {
		Logger.start(true,"count大于库存(库存不足),修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, 10000);
			Logger.debug(productinfomap);
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			Map activityInfo = ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);

			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			// 库存+1
			int inum = Integer.valueOf(productinfomap.get("inum").toString()) + 1;
			Logger.comment("setCount:" + inum);
			changenumbean.setCount(String.valueOf(inum));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE2, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.FAILED201_MSG_2), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_004() {
		Logger.start(true,"count大于商品限购数量(?)，修改购物车中商品数量失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, -1);
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("从mongodb中把刚刚加入到购物车中的数据找出");
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			Logger.comment("设置count为limitnum+1");
			int count = (Integer) productinfomap.get("ilimitnum") + 1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			// 可能会遇到限购的情况 ？
			Logger.verifyEquals(ECode.FAILED201, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE3, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.FAILED201_MSG_9), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_9);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_005() {
		Logger.start(true,"count大于商品活动限购数量(?)，修改购物车中商品数量失败");
		try {
			String uid = TestDataManager.getRandomUUID().toString();
			int isellerid = 1362789;
			Logger.comment("动态给用户添加一条购物车记录，uid：" + uid);
			// 创建产品
			Logger.comment("商家 " + isellerid + " 动态创建一个产品");
			Map productinfomap = ProductInfo.createProduct(isellerid, 5);
			Logger.comment("商家 " + isellerid + " 动态给产品sproductid:" + (String) productinfomap.get("sproductid")
					+ "创建一个活动");
			ActivityInfo.createActivityBySproductid(isellerid, (String) productinfomap.get("sproductid"));
			Logger.comment(uid + ":把刚刚创建的产品加入到购物车中");
			ShoppingCartCallServices.addCatalogToCart(uid, productinfomap);
			Logger.comment("从mongodb中把刚刚加入到购物车中的数据找出");
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			Logger.comment("设置count为2");
			int count = 2;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			// 活动限购默认1 不应该成功
			Logger.verifyEquals(ECode.FAILED201, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE4, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.FAILED201_MSG_8), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_8);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_006() {
		Logger.start(true,"指定购物车条目不属于当前用户(CartId和UserId不匹配),修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			int count = 1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			// 构建一个其他uid2
			String uid2 = TestDataManager.getRandomUUID().toString();
			Logger.comment("setUserId:" + uid2);
			changenumbean.setUserId(uid2);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			// 应该为 此购物车记录Id不属于当前用户 ？
			Logger.verifyEquals(ECode.FAILED201, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE2, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.FAILED201_MSG_4), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_4);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_ChangeCartProductNum_007() {
		Logger.start(true,"输入不存在的CartId,修改购物车失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			// 传入的cartid不存在
			Logger.comment("setCartId:" + cartbean.get_id() + 1);
			changenumbean.setCartId(cartbean.get_id() + 1);
			int count = 1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.FAILED201, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE5, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.FAILED201_MSG_7), "验证返回Msg是否包含字符:"
					+ ECode.FAILED201_MSG_7);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCartProductNum_008() {
		Logger.start(true,"不输入必填项count,修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			// int count=1;
			// Logger.comment("setCount:"+count);
			// changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.ERROR400_MSG_4), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_4);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCartProductNum_009() {
		Logger.start(true,"不输入必填项CartId,修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			// ShopCartMongoBean cartbean =
			// scw.getShoppingCartInfoList("{\"uid\":\""+uid+"\"}",1).get(0);
			// Logger.comment("setCartId:"+cartbean.get_id());
			// changenumbean.setCartId(cartbean.get_id());
			int count = 1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			Logger.comment("setUserId:" + uid);
			changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changenumcall.getBCode(), "验证返回bcode");
			// TODO 这里400错误和其他地方的500错误返回msg一致？需要统一错误码
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.ERROR400_MSG_6), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_6);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeCartProductNum_010() {
		Logger.start(true,"不输入必填项UserId,修改购物车中商品数量失败");
		try {

			String uid = TestDataManager.getRandomUUID().toString();
			int num = ShoppingCartCallServices.addCatalogsToCart(uid, 1);
			Logger.comment("用户" + uid + "成功添加记录数:" + num);
			Logger.comment("从mongodb数据库获取一条uid为'" + uid + "'的购物车记录:" + num);
			ShopCartMongoBean cartbean = scw.getShoppingCartInfoList("{\"uid\":\"" + uid + "\"}", 1).get(0);
			Logger.comment("setCartId:" + cartbean.get_id());
			changenumbean.setCartId(cartbean.get_id());
			int count = 1;
			Logger.comment("setCount:" + count);
			changenumbean.setCount(String.valueOf(count));
			// Logger.comment("setUserId:"+uid);
			// changenumbean.setUserId(uid);
			changenumcall.setData(changenumbean);
			changenumcall.callService();
			Logger.comment("changenumcall send:" + changenumcall.getEntityString());
			Logger.comment("changenumcall return:" + changenumcall.getReturnData());
			Logger.verifyEquals(ECode.ERROR400, changenumcall.getCode(), "验证返回code");
			Logger.verifyEquals(ECode.BCODE0, changenumcall.getBCode(), "验证返回bcode");
			Logger.verifyEquals(true, changenumcall.getMsg().contains(ECode.ERROR400_MSG_1), "验证返回Msg是否包含字符:"
					+ ECode.ERROR400_MSG_1);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}